package net.caixiaomi.info.ui.mine;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.info.widgets.DefLoading;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 实名认证
 */

public class IdentifyActivity extends BaseActivity {

    @BindView(R.id.name)
    EditText mName;

    @BindView(R.id.card)
    EditText mCard;

    /**
     * 认证按钮
     */
    @OnClick(R.id.btn_identify)
    void toSubmit() {
        String name = mName.getText().toString().trim();
        String card = mCard.getText().toString().trim().replace(" ", "");
        if (TextUtils.isEmpty(name)) {
            ToastUtil.s(R.string.C_INPUT_NAME_INVALID);
            return;
        }

        if (!(card.length() == 15 || card.length() == 18)) {
            ToastUtil.s(R.string.C_INPUT_IDENTIFY_CARD);
            return;
        }
        AppHelper.sendStatistics("smrzrenzheng", new String[]{getString(R.string.C_TERMINAL)}, new String[]{Build.BOARD});
        mLoading.show();
        JSONObject params = new JSONObject();
        params.put("idcode", card);
        params.put("realName", name);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<String>> realNameAuth = requestService.realNameAuth(params);
        realNameAuth.enqueue(new ResponseCallback<BaseCallModel<String>>(this) {
            @Override
            public void onSuccess(BaseCallModel data) {
                ToastUtil.s(data.msg);
                mLoading.dismiss();
                onBackPressed();
            }

            @Override
            public void onFail(ResponseError error) {
                mLoading.dismiss();
                if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                    ToastUtil.s(error.getMessage());
                }
            }

            @Override
            public void onError() {
                mLoading.dismiss();
            }
        });


    }

    @Override
    protected int layout() {
        return R.layout.act_identify;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoading = new DefLoading(this);
        mCard.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18)});
        mName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.sendStatistics("smrzshuxingming");
            }
        });
        mName.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AppHelper.sendStatistics("smrzshuxingming");
                return false;
            }
        });
        mCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppHelper.sendStatistics("smrzshushenfenzheng");
            }
        });
        mCard.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AppHelper.sendStatistics("smrzshushenfenzheng");
                return false;
            }
        });
    }
}
